/** 
 * ================================================================================
 * 		Project		: Kompas Bola
 * 		File		: JadwalActivity.java
 * 		Description	: Jadwal Screen
 * 		Called		: MainActivity.java
 * ================================================================================
 * */
package com.kompas.bola.application;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebSettings.ZoomDensity;
import android.widget.ListView;
import android.widget.Toast;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import com.kompas.bola.application.R;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Fields;
import com.kompas.bola.application.adapter.ListJadwalAdapter;
import com.kompas.bola.application.analytics.FlurryUtil;
import com.kompas.bola.application.analytics.GAnalyticsUtil;
import com.kompas.bola.application.dao.Dao;
import com.kompas.bola.application.model.Content;
import com.kompas.bola.application.utils.Constants;
/** =========================================================================== */
public class JadwalActivity extends Activity{	
	private String agenda;
	private ListView list;
	private ListJadwalAdapter adapter;
	private List<Content> list_data					= new ArrayList<Content>();
	private HashMap<String, String> value;
	private WebView imgProgressBar;
	/** ======================================================================= */
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.jadwal_screen);				
		overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left);

		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);
		getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff")));
		getActionBar().setTitle(Constants.EMPTY);
		getActionBar().setLogo(getResources().getDrawable(R.drawable.logo));
		getActionBar().setTitle(Constants.JADWAL_SCREEN);
		
		Intent in 							= getIntent();
		agenda 								= in.getStringExtra(Constants.LIGA);   

		list								= (ListView) findViewById(R.id.list_jadwal);
		adapter								= new ListJadwalAdapter(JadwalActivity.this, list_data);		
		
		imgProgressBar						= (WebView) findViewById(R.id.imgProgressBarJadwal);

		String path							= "file:///android_asset/loading.html";
		imgProgressBar.loadUrl(path);	
		imgProgressBar.getSettings().setDefaultZoom(ZoomDensity.FAR);
		imgProgressBar.setVisibility(View.VISIBLE);

		/** 
		 * START GOOGLE ANALYTICS */
		value													= new HashMap<String, String>();
		value.put(Fields.HIT_TYPE, "appview");
		value.put(Fields.SCREEN_NAME, Constants.JADWAL_SCREEN);
		GAnalyticsUtil.startSessionWithGA(this,value);
		
		/** 
		 * Load Jadwal Task */
		new LoadJadwal().execute(agenda);
	}
	/** =========================================================================== */
	private class LoadJadwal extends AsyncTask<String, Void, List<Content>>{
		protected void onPreExecute(){}

		@Override
		protected List<Content> doInBackground(String... params) {
			// TODO Auto-generated method stub
			String liga						= params[0];
			return Dao.getKlasemenContent(liga);
		}

		protected void onPostExecute(
				List<Content> result){
			if (isCancelled())
				return;		

				if(result.size() > 0){
					imgProgressBar.setVisibility(View.GONE);

	        		list_data.addAll(result);
			        
					try {
						adapter				= new ListJadwalAdapter(JadwalActivity.this, list_data);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						cancel(true);
					}
					
					adapter.notifyDataSetChanged();
					list.setAdapter(adapter);						
				}else{
					Toast.makeText(JadwalActivity.this, Constants.LOW_CONNECTION, Toast.LENGTH_SHORT).show();
				}
		}
	}
	/** =========================================================================== */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
			return true;
		default:
			return super.onOptionsItemSelected(item);		
		}		
	}
	/** =========================================================================== */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
        	Intent main						= new Intent(JadwalActivity.this, MainActivity.class);
            startActivity(main);
			overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }	
	/** =========================== s : Google Analytics ========================== */
	@Override
	public void onStart() {
		super.onStart();		
		
		EasyTracker.getInstance(this).activityStart(this);  // Add this method.		
	    FlurryUtil.startSessionWithFlurry(this, Constants.JADWAL_SCREEN);
	}
	/** =========================================================================== */
	@Override
	public void onStop() {
		super.onStop();

		new LoadJadwal().cancel(true);
	    EasyTracker.getInstance(this).activityStop(this);  // Add this method.
	    FlurryUtil.endSessionWithFlurry(this,  Constants.JADWAL_SCREEN);
	}
	/** =========================== e : Google Analytics ========================== */
}
